package logic

import (
	"context"
	"errors"
	"taobao-merchandise/mysql"

	"taobao-merchandise/taobaorpc/internal/svc"
	"taobao-merchandise/taobaorpc/taobaorpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type GoodsDelLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewGoodsDelLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GoodsDelLogic {
	return &GoodsDelLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// 商品删除
func (l *GoodsDelLogic) GoodsDel(in *taobaorpc.GoodsDelRequest) (*taobaorpc.GoodsDelResponse, error) {
	// todo: add your logic here and delete this line

	var user mysql.User
	if err := user.FindUserById(mysql.DB, in.UserId); err != nil {
		return nil, errors.New("用户不存在")
	}

	var goods mysql.Goods
	err := goods.FindGoods(mysql.DB, in.GoodsId)
	if err != nil {
		return nil, errors.New("商品不存在")
	}

	if goods.UserId != in.UserId {
		return nil, errors.New("商品无法删除")
	}

	err = goods.GoodsDel(mysql.DB, in.GoodsId)
	if err != nil {
		return nil, errors.New("删除失败")
	}

	return &taobaorpc.GoodsDelResponse{}, nil
}
